package cn.com.shopec.erp.base.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.shopec.erp.base.dao.ISupplierAccountsDao;
import cn.com.shopec.erp.base.dao.ISupplierInfoDao;
import cn.com.shopec.erp.base.entity.SupplierAccounts;
import cn.com.shopec.erp.base.entity.SupplierInfo;
import cn.com.shopec.erp.base.service.ISupplierAccountsService;
import cn.com.shopec.erp.base.service.ISupplierInfoService;
import cn.com.shopec.erp.base.vo.SupplierVo;
import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.excep.WMSException;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.page.PageQuery;
import cn.com.shopec.erp.common.utils.DateUtil;
import cn.com.shopec.erp.common.utils.LogicUtil;
import cn.com.shopec.erp.common.utils.MD5Util;
import cn.com.shopec.erp.common.utils.StringUtil;
import cn.com.shopec.erp.financing.entity.WhareHouse;
import cn.com.shopec.erp.provideinterface.service.ISupplierInfoWMSService;

@Service
public class SupplierInfoService implements ISupplierInfoService{
	
	/** 日志. */
	private Logger log = Logger.getLogger(SupplierInfoService.class);
	
	@Resource
	private ISupplierInfoDao supplierInfoDao;
	
	@Resource
	private ISupplierAccountsDao supplierAccountsDao;
	
	@Resource
	private ISupplierAccountsService supplierAccountsService;
	
//	@Resource
//	private ISupplierInfoWMSService supplierInfoWMSService;
	
	/**
	 * 新增供应商信息
	 */
	@Override
	@Transactional
	public SupplierInfo addSupplierInfo(SupplierVo vo) throws Exception {
		
		//供应商基本信息
		SupplierInfo sup = new SupplierInfo();
		sup.setAccount(vo.getAccount());                               //帐号
		String md5password=MD5Util.MD5Encode(vo.getAccount(),"");
		sup.setPassword(md5password);
		sup.setAddress(vo.getAddress());                               //地址
		sup.setDeleteFlag(1);                                          //0：已删除，1：未删除
		sup.setEmail(vo.getEmail());                                   //电子邮箱
		sup.setLinkman(vo.getLinkman());                               //联系人
		sup.setMobileNhonenum(vo.getMobileNhonenum());                 //手机号码
		sup.setSupCode(vo.getSupCode());                               //供应商编码
		sup.setSupName(vo.getSupName());                               //供应商名称
		sup.setSupShortName(vo.getSupShortName());                     //供应商简称
		sup.setTelephoneNum(vo.getTelephoneNum());                     //联系电话
		sup.setRegisterDate(DateUtil.formatTimesTampDate(new Date())); //注册时间
		sup.setState(SZSCMConstant.STATE_LOCK);                        //解锁
		sup.setCode(SZSCMConstant.genUUID());
		sup.setUpdateTime(DateUtil.formatTimesTampDate(new Date()));//修改时间
		sup.setOperateTpye(vo.getOperateTpye());//经营类别
		sup.setContractDayStrat(vo.getContractDayStrat());
		sup.setContractDayEnd(vo.getContractDayEnd());
	
//		//同步供应商信息到WMS
//		boolean flag = false;
//
//		try 
//		{
//			log.info("------SupplierInfo----------start----------");
//			flag = supplierInfoWMSService.supplierCreateOrUpadteWMS(sup);
//		}
//		catch (Exception e)
//		{
//			log.info("------SupplierInfo----------fail|exception----------");
//			log.error(e.getMessage());
//			throw new WMSException("WMS"+e.getMessage());
//		}
//		if(flag)
//		{
			log.info("------SupplierInfo----------success----------");		
			 supplierInfoDao.save(sup);
			JSONArray jsonConArr = JSONArray.fromObject(vo.getConBrandArr());
			JSONArray jsonPurArr = JSONArray.fromObject(vo.getPurBrandArr());
			if (LogicUtil.isNotNullAndEmpty(jsonConArr)) {
				for (int i=0;i<jsonConArr.size();i++) {
					JSONObject accountObject = jsonConArr.getJSONObject(i);
				    SupplierAccounts accounts = new SupplierAccounts();
				    accounts.setAccountsTime(accountObject.getString("accountsTime"));
				    accounts.setAccountsWay(accountObject.getString("accountsWay"));
				    accounts.setPointsScale(accountObject.getString("pointsScale"));
				    accounts.setBrandName(accountObject.getString("brandName"));
				    accounts.setBrandCode(accountObject.getString("brandNo"));
				    accounts.setAccountsType("2"); // 2：代销
				    accounts.setSupplierInfo(sup);
				    accounts.setDeleteFlag(SZSCMConstant.DELETE_FLAG_NO);
				    supplierAccountsDao.save(accounts);
				}
			}
			if (LogicUtil.isNotNullAndEmpty(jsonPurArr)) {
				for (int i=0;i<jsonPurArr.size();i++) {
					JSONObject accountObject = jsonPurArr.getJSONObject(i);
				    SupplierAccounts accounts = new SupplierAccounts();
				    accounts.setAccountsTime(accountObject.getString("accountsTime")); 
				    accounts.setBrandName(accountObject.getString("brandName"));
				    accounts.setBrandCode(accountObject.getString("brandNo"));
				    accounts.setAccountsType("1"); // 1:购销
				    accounts.setSupplierInfo(sup);
				    accounts.setDeleteFlag(SZSCMConstant.DELETE_FLAG_NO);
				    supplierAccountsDao.save(accounts);
				}
			}
//		}
		
		return sup;
		
	}

	/**
	 * 修改供应商信息
	 * @throws Exception 
	 */
	@Override
	@Transactional
	public String updateSupplierInfo(SupplierVo vo,String aslist) throws Exception {
		//供应商基本信息
		SupplierInfo sup = getSupplierInfoCode(vo.getCode());
		sup.setAddress(vo.getAddress());                                //地址
		//sup.setContractDay(vo.getContractDay());                       //合同周期
		sup.setDeleteFlag(SZSCMConstant.DELETE_FLAG_NO);              //0：已删除，1：未删除
		sup.setEmail(vo.getEmail());                                 //电子邮箱
		sup.setLinkman(vo.getLinkman());                            //联系人
		sup.setMobileNhonenum(vo.getMobileNhonenum());             //手机号码
		sup.setSupCode(vo.getSupCode());                          //供应商编码
		sup.setSupName(vo.getSupName());                         //供应商名称
		sup.setSupShortName(vo.getSupShortName());              //供应商简称
		sup.setTelephoneNum(vo.getTelephoneNum());             //联系电话
		sup.setRegisterDate(vo.getRegisterDate());            //注册时间
		sup.setCode(vo.getCode());
		sup.setUpdateTime(DateUtil.formatTimesTampDate(new Date()));//修改时间
		sup.setOperateTpye(vo.getOperateTpye());//经营类别
		sup.setContractDayStrat(vo.getContractDayStrat());//合同开始日期
		sup.setContractDayEnd(vo.getContractDayEnd());//合同结束日期
		
//		boolean flag = false;
//
//		try {
//			log.info("------SupplierInfo----------start----------");
//			flag = supplierInfoWMSService.supplierCreateOrUpadteWMS(sup);
//		} catch (Exception e) {
//			log.info("------SupplierInfo----------fail|exception----------");
//			log.error(e.getMessage());
//			throw new WMSException("WMS"+e.getMessage());
//		}
//		if(flag)
//		{
			log.info("------SupplierInfo----------success----------");
			supplierInfoDao.update(sup);
			Query query = supplierAccountsDao.createSQLQuery("DELETE FROM TBL_SUPPLIER_ACCOUNTS WHERE CODE='" + sup.getCode() + "'");
			query.executeUpdate();
			JSONArray jsonConArr = JSONArray.fromObject(vo.getConBrandArr());
			JSONArray jsonPurArr = JSONArray.fromObject(vo.getPurBrandArr());
			if (LogicUtil.isNotNullAndEmpty(jsonConArr)) {
				for (int i=0;i<jsonConArr.size();i++) {
					JSONObject accountObject = jsonConArr.getJSONObject(i);
				    SupplierAccounts accounts = new SupplierAccounts();
				    accounts.setAccountsTime(accountObject.getString("accountsTime"));
				    accounts.setAccountsWay(accountObject.getString("accountsWay"));
				    accounts.setPointsScale(accountObject.getString("pointsScale"));
				    accounts.setBrandName(accountObject.getString("brandName"));
				    accounts.setBrandCode(accountObject.getString("brandNo"));
				    accounts.setAccountsType("2"); // 2：代销
				    accounts.setSupplierInfo(sup);
				    accounts.setDeleteFlag(SZSCMConstant.DELETE_FLAG_NO);
				    supplierAccountsDao.save(accounts);
				}
			}
			if (LogicUtil.isNotNullAndEmpty(jsonPurArr)) {
				for (int i=0;i<jsonPurArr.size();i++) {
					JSONObject accountObject = jsonPurArr.getJSONObject(i);
				    SupplierAccounts accounts = new SupplierAccounts();
				    accounts.setAccountsTime(accountObject.getString("accountsTime")); 
				    accounts.setBrandName(accountObject.getString("brandName"));
				    accounts.setBrandCode(accountObject.getString("brandNo"));
				    accounts.setAccountsType("1"); // 1:购销
				    accounts.setSupplierInfo(sup);
				    accounts.setDeleteFlag(SZSCMConstant.DELETE_FLAG_NO);
				    supplierAccountsDao.save(accounts);
				}
			}
			return SZSCMConstant.SUCCESS;
//		}else{
//			return SZSCMConstant.FAIL;
//		}
	}
	
	/**
	 * 分页查询供应商信息
	 */
	@Override
	public PageFinder<SupplierInfo> querySupplierInfos(SupplierInfo supplierInfo,PageQuery pageQuery) {
	
		DetachedCriteria detachedCriteria = supplierInfoDao.createCriteria();
		
		//供应商名称
		if(null != supplierInfo && LogicUtil.isNotNullAndEmpty(supplierInfo.getSupName())){
			detachedCriteria.add(Restrictions.like("supName", supplierInfo.getSupName(),MatchMode.ANYWHERE));
		}
		//供应商账号
		if(null != supplierInfo && LogicUtil.isNotNullAndEmpty(supplierInfo.getAccount())){
			detachedCriteria.add(Restrictions.like("account", supplierInfo.getAccount(),MatchMode.ANYWHERE));
		}
		
		//未失效
		if(null != supplierInfo && LogicUtil.isNotNullAndEmpty(supplierInfo.getState())){
			detachedCriteria.add(Restrictions.eq("state", supplierInfo.getState()));
		}
		
		if(null != supplierInfo && StringUtil.isExist(supplierInfo.getCreateStartDate())){
			detachedCriteria.add(Restrictions.ge("registerDate", supplierInfo.getCreateStartDate()));
		}
		if(null != supplierInfo && StringUtil.isExist(supplierInfo.getCreateEndDate())){
			detachedCriteria.add(Restrictions.le("registerDate", supplierInfo.getCreateEndDate()));
		}
		
		//未删除
		detachedCriteria.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
		//根据注册时间排序
		detachedCriteria.addOrder(Order.desc("registerDate"));
		
		return supplierInfoDao.pagedByCriteria(detachedCriteria, pageQuery.getPage(), pageQuery.getPageSize());
	}
	
	
	/**
	 * 分页查询供应商信息
	 */
	@Override
	public PageFinder<SupplierInfo> querySupplier(SupplierVo supplierVo,PageQuery pageQuery) {
	
		DetachedCriteria detachedCriteria = supplierInfoDao.createCriteria();
		
		//过滤供应商过期
		/*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
		String dateNowStr = sdf.format(new Date());
		String hql = "";
		if(null != supplierVo){
			hql = " where  this.contractDayEnd > '"+dateNowStr+"' ";
			detachedCriteria.add(Restrictions.sqlRestriction(hql));
//			DateUtil.parseTimesTampDate(SupplierInfo.contractDayEnd())
			
		}*/
		
		//供应商名称
		if(null != supplierVo && LogicUtil.isNotNullAndEmpty(supplierVo.getSupName())){
			detachedCriteria.add(Restrictions.like("supName", supplierVo.getSupName(),MatchMode.ANYWHERE));
		}
		
		
		//供应商账号
		if(null != supplierVo && LogicUtil.isNotNullAndEmpty(supplierVo.getAccount())){
			detachedCriteria.add(Restrictions.like("account", supplierVo.getAccount(),MatchMode.ANYWHERE));
		}
		
		//未失效
		if(null != supplierVo && LogicUtil.isNotNullAndEmpty(supplierVo.getState())){
			detachedCriteria.add(Restrictions.eq("state", supplierVo.getState()));
		}
		
		String sql = "";
		if(StringUtil.isExist(supplierVo.getBrandNo())){
			sql += " sa.brand_code= (select br.no from tbl_brand br where br.brand_no='"+supplierVo.getBrandNo()+"') ";
		}
		
		if(StringUtil.isExist(supplierVo.getSaleType())){
			if(sql != "") sql += " and ";
			sql += " sa.accounts_type="+supplierVo.getSaleType();
		}
		
		if(sql != ""){
			sql = "this_.code in (select sa.code from TBL_SUPPLIER_ACCOUNTS sa where " + sql +")";
			detachedCriteria.add(Restrictions.sqlRestriction(sql));
		}
		
		//未删除
		detachedCriteria.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
		//根据注册时间排序
		detachedCriteria.addOrder(Order.desc("registerDate"));
		
		return supplierInfoDao.pagedByCriteria(detachedCriteria, pageQuery.getPage(), pageQuery.getPageSize());
	}
	
	/**
	 * 根据供应商编号查询供应商信息
	 * update gaoyaxiong 2013-6-14
	 */
	@Override
	public SupplierInfo getSupplierInfoBySuppCode(String code) {
		DetachedCriteria detachedCriteria = supplierInfoDao.createCriteria();
		detachedCriteria.add(Restrictions.eq("code", code));
		detachedCriteria.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
		List<SupplierInfo> supps = supplierInfoDao.findByCriteria(detachedCriteria);
		
		if(null!=supps && supps.size()>0){
			return supps.get(0);
		}
		return null;
	}

	/**
	 * 根据供应商code获取供应商信息
	 * @throws Exception 
	 */
	@Override
	@Transactional(readOnly=true)
	public SupplierInfo getSupplierInfoCode(String code) throws Exception {
		return supplierInfoDao.findUniqueBy("code", code);
	}

	@Override
	@Transactional
	public void delSupplier(String code) throws Exception {
		if(LogicUtil.isNotNullAndEmpty(code)){
			SupplierInfo info = getSupplierInfoCode(code);
			List<SupplierAccounts> list = supplierAccountsService.getSupplierAccountsListByCode(code);
			
			if(null != list && list.size()>0)
			{
				for(SupplierAccounts ac : list)
				{
					ac.setDeleteFlag(SZSCMConstant.DELETE_FLAG_YES);//已删除
					supplierAccountsDao.update(ac);
				}
				
				info.setDeleteFlag(SZSCMConstant.DELETE_FLAG_YES);//已删除
			}
			else
			{
				info.setDeleteFlag(SZSCMConstant.DELETE_FLAG_YES);//已删除
			}
			    supplierInfoDao.update(info);
		}
	}

	@Override
	public List<SupplierInfo> getAllSupplierInfo() {
		DetachedCriteria detachedCriteria = supplierInfoDao.createCriteria();
		detachedCriteria.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
		return supplierInfoDao.findByCriteria(detachedCriteria);
	}

	@Override
	public List<SupplierVo> getSupplierVoBysupCodeAndaccountsType(SupplierVo vo) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
	
	   DetachedCriteria de = supplierAccountsDao.createCriteria();
	   de.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
	   de.add(Restrictions.eq("supCode", vo.getSupCode()));
	   de.add(Restrictions.eq("accountsType", vo.getAccountsType()));//
	   List<SupplierAccounts> accountsList = supplierAccountsDao.findByCriteria(de);
	   List<SupplierVo> voList = new ArrayList<SupplierVo>();
	   if(null!=accountsList && accountsList.size()>0){ 
		   for(SupplierAccounts ac: accountsList){
			   SupplierVo supplierVo = new SupplierVo();
			   PropertyUtils.copyProperties(supplierVo, ac);
			   try{
				   SupplierAccounts sp = supplierAccountsService.getSupplierAccountsListByCode(ac.getSupplierInfo().getCode()).get(0);
				   if(null!=sp && sp.getAccountsType().equals("1")){
					   supplierVo.setAccountsTimeBygou(sp.getAccountsTime());//购销,代销结算日期 
				   }
				   if(null!=sp && sp.getAccountsType().equals("2")){
					   supplierVo.setAccountsTimeBydai(sp.getAccountsTime());//代销结算日期 
				   }
				   supplierVo.setAccountsWay(sp.getAccountsWay());
				   supplierVo.setPointsScale(sp.getPointsScale());//扣点比例
			   }catch(Exception e){}
			   voList.add(supplierVo);
		   }
	   }
		return voList;
	}

	@Override
	public SupplierVo getSupplierVo(SupplierVo  supVo) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException  {
		   SupplierInfo supp = getSupplierInfoBySuppCode(supVo.getCode());
		   SupplierVo sVo =null;
		   DetachedCriteria de = supplierAccountsDao.createCriteria();
		   if(null != supp){
			   de.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
			   de.createAlias("supplierInfo", "supplierInfo").add(Restrictions.eq("supplierInfo.code", supp.getCode()));
			   if(LogicUtil.isNotNullAndEmpty(supVo.getCode())){
				   de.add(Restrictions.eq("accountsType", supVo.getCode()));//结算类型
			   }
		   }
		   List<SupplierAccounts> accountsList = supplierAccountsDao.findByCriteria(de);
		   List<SupplierVo> voList = new ArrayList<SupplierVo>();
		   if(null!=accountsList && accountsList.size()>0){ 
			   for(SupplierAccounts ac: accountsList){
				   SupplierVo supplierVo = new SupplierVo();
				   PropertyUtils.copyProperties(supplierVo, ac);
				   try{
					   SupplierAccounts sp = supplierAccountsService.getSupplierAccountsListByCode(ac.getSupplierInfo().getCode()).get(0);
					   if(null!=sp && sp.getAccountsType().equals("1")){
						   supplierVo.setAccountsTimeBygou(sp.getAccountsTime());//购销,代销结算日期 
					   }
					   if(null!=sp && sp.getAccountsType().equals("2")){
						   supplierVo.setAccountsTimeBydai(sp.getAccountsTime());//代销结算日期 
					   }
					   supplierVo.setAccountsWay(sp.getAccountsWay());
					   supplierVo.setPointsScale(sp.getPointsScale());//扣点比例
				   }catch(Exception e){}
				   voList.add(supplierVo);
			   }
			   if(null != voList && voList.size()>0){
				   sVo = voList.get(0);
			   }
		   }
			
		return sVo;
	}
              
     /**
      * 根据供应商帐号查询供应商
      * 
      * @param supplierInfo
      * @return
      * @throws Exception
      ***/
     public  List<SupplierInfo> getSupplierAccount(String account) throws Exception
     {
          DetachedCriteria detachedCriteria = supplierInfoDao.createCriteria();
          detachedCriteria.add(Restrictions.eq("account", account));
          detachedCriteria.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
          return  supplierInfoDao.findByCriteria(detachedCriteria);
     }
     
     /**
      * 根据供应商帐号查询供应商
      * 
      * @param supplierInfo
      * @return
      * @throws Exception
      ***/
     public  List<SupplierInfo> checkSupName(String supName) throws Exception
     {
          DetachedCriteria detachedCriteria = supplierInfoDao.createCriteria();
          detachedCriteria.add(Restrictions.eq("supName", supName));
          detachedCriteria.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
          return  supplierInfoDao.findByCriteria(detachedCriteria);
     }

     
	@Override
	public void updateState(SupplierInfo supplierInfo) throws Exception {
		SupplierInfo info = getSupplierInfoCode(supplierInfo.getCode());
		info.setState(supplierInfo.getState());
		 supplierInfoDao.update(info);
	}

     /**
      * 修改密码
      * */
     @Transactional
     public void updatePassword(SupplierInfo info)
     {
          try
          {
               supplierInfoDao.update(info);
          }
          catch (Exception e)
          {
              
               e.printStackTrace();
          }
          
     }

	@Override
	public SupplierVo getSupplierVoByLoginName(String account) throws Exception {
		DetachedCriteria criteria = supplierInfoDao.createCriteria();
		criteria.add(Restrictions.eq("account", account));
		criteria.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
		List<SupplierInfo> supps = supplierInfoDao.findByCriteria(criteria);
		SupplierInfo supp = null;
		if (null != supps && supps.size() > 0) {
			supp = supps.get(0);
		}
		if (null == supp) {
			return null;
		}
		DetachedCriteria de = supplierAccountsDao.createCriteria();
		de.createAlias("supplierInfo", "supp");
		de.add(Restrictions.eq("supp.code", supp.getCode()));
		de.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
		List<SupplierAccounts> accountList = supplierAccountsDao.findByCriteria(de);
		SupplierVo suppVo = new SupplierVo();
		suppVo.setId(supp.getId());
		suppVo.setContractDayStrat(supp.getContractDayStrat());
		suppVo.setAccount(supp.getAccount());
		suppVo.setPassword(supp.getPassword());
		suppVo.setCode(supp.getCode());
		suppVo.setSupCode(supp.getSupCode());
		suppVo.setDeleteFlag(supp.getDeleteFlag());
		suppVo.setState(supp.getState());//0:解锁 1：锁定
		int aType = 0;
		int bType = 0;
		if (null != accountList && accountList.size() > 0) {
			for (SupplierAccounts suppAccount : accountList) {
				if (suppAccount.getAccountsType().equals("1") && StringUtil.isExist(suppAccount.getAccountsTime())) {
					suppVo.setAccountsType("1"); // 购销
					aType ++;
				} else if(suppAccount.getAccountsType().equals("2") && StringUtil.isExist(suppAccount.getAccountsTime())) {
					suppVo.setAccountsType("2"); // 代销
					bType ++;
				}
			}
		}
		if (aType > 0 && bType > 0) {
			suppVo.setAccountsType("3"); // 购销和代销
		}
		return suppVo;
	}


	@Override
	public List<SupplierInfo> getSupplyInfoByTimeArea(String startTime,
			String endTime) throws Exception {
		DetachedCriteria detachedCriteria = supplierInfoDao.createCriteria();
		detachedCriteria.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
		//detachedCriteria.add(Restrictions.between("updateTime", startTime, endTime));		
		return supplierInfoDao.findByCriteria(detachedCriteria);
	}

	@Override
	@Transactional
	public void updatePwd(SupplierInfo supplierInfo) throws Exception {
		if(null!=supplierInfo){
	    	  if(StringUtil.isExist(supplierInfo.getCode())&& StringUtil.isExist(supplierInfo.getPassword())){
	    		  SupplierInfo info = getSupplierInfoCode(supplierInfo.getCode());
	    		  String password=supplierInfo.getPassword();
	    		  String pass=MD5Util.MD5Encode(password, "");
	    		  info.setPassword(pass);
	    		  supplierInfoDao.update(info);
	    	  }
	     }
	}


	@Override
	@Transactional
	public void supplierFailure(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String time = sdf.format(new Date());
		try {
			int i = this.supplierInfoDao.updateSupplierFailure(time);
			log.info("有"+i+"个供应商合同过期失效");
		} catch (HibernateException e) {
			log.info("更新供应商合同过期失效："+e.getMessage());
		}
		
	}
	
	@Override
	public List<?> getExportExcel(SupplierVo supVo){
		return this.supplierInfoDao.getExportExcel(supVo);
	}

	@Override
	public PageFinder<SupplierInfo> getPagedCollectSetList(PageQuery pageQuery) {

	 DetachedCriteria criteria = supplierInfoDao.createCriteria();    
   	 //数据有效。。未被删除
   	 criteria.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO)); 
   	 PageFinder<SupplierInfo>  pf= supplierInfoDao.pagedByCriteria(criteria, pageQuery.getPage(), pageQuery.getPageSize());    	
		return pf;
	
	}
	
	/**
	 * 根据供应商id查询所对应的
	 */
	@Override
	public SupplierInfo getSupplierInfoById(String supCode) {
		return supplierInfoDao.findUniqueBy("code", supCode);
	}
	
	/**
	 * 根据供应商id查询所对应的
	 */
	@Override
	public SupplierInfo getSupplierInfoBySupName(String suppName) {
		return supplierInfoDao.findUniqueBy("supName", suppName);
	}
	
}